﻿using System;
using System.IO.IsolatedStorage;
using System.IO;
using System.Text;

namespace E_Surfing.WP8Framework.File
{
    public class WPFileHandle
    {
       
        static WPFileHandle()
        {
            
        }

        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string CreateFile(string fileName)
        {
            try
            {
                WPFile file = new WPFile();
                if (string.IsNullOrEmpty(fileName))
                {
                    return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
                }
                file.CreateFile(fileName);
                return WPResultCode.RESTLT_OK;
            }
            catch (IsolatedStorageException ex)
            {
                throw (ex);
            }
            catch (ArgumentException ex)
            {
                throw (ex);
            }
            catch (DirectoryNotFoundException ex)
            {
                throw (ex);
            }
            catch (ObjectDisposedException ex)
            {
                throw (ex);
            }
        }

        /// <summary>
        /// 重载：创建文件
        /// </summary>
        /// <param name="dirName"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string CreateFile(string dirName, string fileName)
        {
            WPFile file = new WPFile();
            if (string.IsNullOrEmpty(dirName) || string.IsNullOrEmpty(fileName))
            {
                return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
            }
            if (!file.DirectoryExist(dirName))
            {
                file.CreateDirectory(dirName);
            }

            StringBuilder sb = new StringBuilder();
            sb.Append(dirName);
            sb.Append("\\");
            sb.Append(fileName);

            return CreateFile(sb.ToString());
        }

        /// <summary>
        /// 在独立存储范围中创建目录
        /// </summary>
        /// <param name="dirName"></param>
        public static string CreateDirectory(string dirName)
        {
            try
            {
                WPFile file = new WPFile();
                if (string.IsNullOrEmpty(dirName))
                {
                    return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
                }
                if (!DirectoryExist(dirName))
                {
                    file.CreateDirectory(dirName);
                }
                return WPResultCode.RESTLT_OK;
            }
            catch (IsolatedStorageException ex)
            { 
                throw(ex);
            }
            catch (ArgumentException ex)
            {
                throw(ex);
            }
            catch (DirectoryNotFoundException ex)
            {
                throw(ex);
            }
            catch (ObjectDisposedException ex)
            {
                throw(ex);
            }
        }

        /// <summary>
        /// 删除独立存储范围中的目录 && 删除目录里的所有文件 目前不支持目录里还有目录的删除
        /// </summary>
        /// <param name="dirName">要在独立存储范围中删除的目录的相对路径</param>
        public static string DeleteDirectory(string dirName)
        {
            try
            {
                WPFile file = new WPFile();
                if (string.IsNullOrEmpty(dirName))
                {
                    return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
                }
                if (DirectoryExist(dirName))
                {
                    file.DeleteDirectory(dirName);
                }
                return WPResultCode.RESTLT_OK;
            }
            catch (IsolatedStorageException ex)
            {
                throw(ex);
            }
            catch (ArgumentException ex)
            {
                throw(ex);
            }
            catch (DirectoryNotFoundException ex)
            {
                throw(ex);
            }
            catch (ObjectDisposedException ex)
            {
                
                throw(ex);
            }
        }

        /// <summary>
        /// 删除独立存储区中的文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string DeleteFile(string fileName)
        {
            try
            {
                WPFile file = new WPFile();
                if (string.IsNullOrEmpty(fileName))
                {
                    return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
                }
                if (FileExist(fileName))
                {
                    file.DeleteFile(fileName);
                    return WPResultCode.RESTLT_OK;
                }
                return WPResultCode.RESTLT_OK;
            }
            catch (IsolatedStorageException ex)
            {
                
                throw(ex);
            }
            catch (ArgumentException ex)
            {
                
                throw(ex);
            }
            catch (DirectoryNotFoundException ex)
            {
                
                throw(ex);
            }
            catch (ObjectDisposedException ex)
            {
                
                throw(ex);
            }
        }

        /// <summary>
        /// 删除独立存储区中的文件
        /// </summary>
        /// <param name="dirName"></param>
        /// <param name="fileName"></param>
        public static string DeleteFile(string dirName, string fileName)
        {
            if (string.IsNullOrEmpty(dirName) || string.IsNullOrEmpty(fileName))
            {
                return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
            }

            StringBuilder sb = new StringBuilder();
            sb.Append(dirName);
            sb.Append("\\");
            sb.Append(fileName);
            return DeleteFile(sb.ToString());
        }

        /// <summary>
        /// 确定指定的路径是否指的是独立存储区中的现有文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool FileExist(string fileName)
        {
            if(string.IsNullOrEmpty(fileName))
            {
                return false;
            }
            
            try
            {
                WPFile file = new WPFile();
                return file.FileExist(fileName);
            }
            catch (IsolatedStorageException ex)
            {
                
                throw(ex);
            }
            catch (ObjectDisposedException ex)
            {
                
                throw(ex);
            }
        }

        /// <summary>
        /// 确定指定的路径是否指的是独立存储区中的现有文件
        /// </summary>
        /// <param name="dirName"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool FileExist(string dirName, string fileName)
        {
            if (string.IsNullOrEmpty(dirName) || string.IsNullOrEmpty(fileName))
            {
                return false;
            }

            StringBuilder sb = new StringBuilder();
            sb.Append(dirName);
            sb.Append("\\");
            sb.Append(fileName);
            return FileExist(sb.ToString());
        }

        /// <summary>
        /// 删除独立存储范围中的目录
        /// </summary>
        /// <param name="dirName"></param>
        /// <returns></returns>
        public static bool DirectoryExist(string dirName)
        {
            try
            {
                WPFile file = new WPFile();
                if (string.IsNullOrEmpty(dirName))
                {
                    return false;
                }
                return file.DirectoryExist(dirName);
            }
            catch (IsolatedStorageException ex)
            {
                
                throw (ex);
            }
            catch (ArgumentException ex)
            {
                
                throw (ex);
            }
            catch (DirectoryNotFoundException ex)
            {
                
                throw (ex);
            }
            catch (ObjectDisposedException ex)
            {
                
                throw (ex);
            }
        }

        /// <summary>
        /// 在指定的模式中打开文件 用于写
        /// </summary>
        /// <param name="path"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static IsolatedStorageFileStream OpenFileForWrite(string path, FileMode mode)
        {
            try
            {
                WPFile file = new WPFile();
                if (string.IsNullOrEmpty(path))
                {
                    return null;
                }
                return file.OpenFileForWrite(path, mode);
            }
            catch (IsolatedStorageException ex)
            {
                
                throw (ex);
            }
            catch (ArgumentException ex)
            {
                
                throw (ex);
            }
            catch (DirectoryNotFoundException ex)
            {
                
                throw (ex);
            }
            catch (FileNotFoundException ex)
            {
                
                throw (ex);
            }
            catch (ObjectDisposedException ex)
            {
                
                throw (ex);
            }
        }

        /// <summary>
        /// 在指定的模式中打开文件 用于读
        /// </summary>
        /// <param name="path"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static IsolatedStorageFileStream OpenFileForRead(string path, FileMode mode)
        {
            try
            {
                WPFile file = new WPFile();
                if (string.IsNullOrEmpty(path))
                {
                    return null;
                }
                return file.OpenFileForRead(path, mode);
            }
            catch (IsolatedStorageException ex)
            {
                
                throw (ex);
            }
            catch (ArgumentException ex)
            {
                
                throw (ex);
            }
            catch (DirectoryNotFoundException ex)
            {
                
                throw (ex);
            }
            catch (FileNotFoundException ex)
            {
                
                throw (ex);
            }
            catch (ObjectDisposedException ex)
            {
                
                throw (ex);
            }
        }

        /// <summary>
        /// 在指定的模式中打开文件 用于读写
        /// </summary>
        /// <param name="path"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static IsolatedStorageFileStream OpenFileForReadWrite(string path, FileMode mode)
        {
            try
            {
                WPFile file = new WPFile();
                if (string.IsNullOrEmpty(path))
                {
                    return null;
                }
                return file.OpenFileForReadWrite(path, mode);
            }
            catch (IsolatedStorageException ex)
            {
                
                throw (ex);
            }
            catch (ArgumentException ex)
            {
                
                throw (ex);
            }
            catch (DirectoryNotFoundException ex)
            {
                
                throw (ex);
            }
            catch (FileNotFoundException ex)
            {
                
                throw (ex);
            }
            catch (ObjectDisposedException ex)
            {
                
                throw (ex);
            }
        }

        /// <summary>
        /// 将IsolatedStorageFileStream对象偏移
        /// </summary>
        /// <param name="path"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static string SeekFilePosition(ref IsolatedStorageFileStream isoStream, long offset)
        {
            try
            {
                WPFile file = new WPFile();
                if (isoStream == null)
                {
                    return WPResultCode.RESULT_IN_OBJECT_IS_NULL;
                }
                if (offset < 0)
                {
                    return WPResultCode.RESULT_FILE_SEEK_LESS_ZERO;
                }
                if (file.SeekFilePosition(ref isoStream, offset) >= 0)
                {
                    return WPResultCode.RESTLT_OK;
                }
                return WPResultCode.RESULT_FAULT;
            }
            catch (Exception ex)
            {
                
                throw (ex);
            }
        }

        /// <summary>
        /// 使用从字节数组中读取的数据将字节块写入 IsolatedStorageFileStream 对象
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string WriteStreamToFile(string fileName, byte[] bytes)
        {
            try
            {
                WPFile file = new WPFile();
                if (!DiskSpaceIsEnough(bytes.Length))
                {
                    return WPResultCode.RESULT_DISK_SPACE_NOT_ENOUGH;
                }
                if (string.IsNullOrEmpty(fileName))
                {
                    return WPResultCode.RESULT_FILE_NOT_EXIST;
                }
                if (file.WriteStreamToFile(fileName, bytes))
                {
                    return WPResultCode.RESTLT_OK;
                }
                else
                {
                    return WPResultCode.RESULT_FAULT;
                }
            }
            catch (IsolatedStorageException ex)
            { 
                
                throw (ex);
            }
        }

        /// <summary>
        /// 使用从字节数组中读取的数据将字节块写入 IsolatedStorageFileStream 对象
        /// </summary>
        /// <param name="dirName"></param>
        /// <param name="fileName"></param>
        /// <param name="bytes"></param>
        public static string WriteStreamToFile(string dirName, string fileName, byte[] bytes)
        {
            if (!DirectoryExist(dirName))
            {
                string rCode = CreateDirectory(dirName);
                if (rCode != WPResultCode.RESTLT_OK)
                {
                    return rCode;
                }
            }

            StringBuilder sb = new StringBuilder();
            sb.Append(dirName);
            sb.Append("\\");
            sb.Append(fileName);

            return WriteStreamToFile(sb.ToString(), bytes);
        }

        /// <summary>
        /// 使用从string块写入 IsolatedStorageFileStream 对象
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string WriteStringToFile(string fileName, string strData)
        {
            try
            {
                if (string.IsNullOrEmpty(fileName))
                {
                    return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
                }
                byte[] byteArray = Encoding.Unicode.GetBytes(strData);
                if (!DiskSpaceIsEnough(byteArray.Length))
                {
                    return WPResultCode.RESULT_DISK_SPACE_NOT_ENOUGH;
                }

                return WriteStreamToFile(fileName, byteArray);
            }
            catch (IsolatedStorageException ex)
            {
                
                throw (ex);
            }
        }

        /// <summary>
        /// hrc
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="strData"></param>
        /// <returns></returns>
        public static string WriteStringToNewFile(string fileName, string strData)
        {
            try
            {
                if (string.IsNullOrEmpty(fileName))
                {
                    return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
                }
                byte[] byteArray = Encoding.Unicode.GetBytes(strData);
                if (!DiskSpaceIsEnough(byteArray.Length))
                {
                    return WPResultCode.RESULT_DISK_SPACE_NOT_ENOUGH;
                }

                return WriteStreamToNewFile(fileName, byteArray);
            }
            catch (IsolatedStorageException ex)
            {
                
                return WPResultCode.RESULT_STORE_FOR_APP_ERR;
            }
        }

        /// <summary>
        /// hrc
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string WriteStreamToNewFile(string fileName, byte[] bytes)
        {
            try
            {
                WPFile file = new WPFile();
                if (!DiskSpaceIsEnough(bytes.Length))
                {
                    return WPResultCode.RESULT_DISK_SPACE_NOT_ENOUGH;
                }
                if (string.IsNullOrEmpty(fileName))
                {
                    return WPResultCode.RESULT_FILE_NOT_EXIST;
                }
                if (file.WriteStreamToNewFile(fileName, bytes))
                {
                    return WPResultCode.RESTLT_OK;
                }
                else
                {
                    return WPResultCode.RESULT_FAULT;
                }
            }
            catch (IsolatedStorageException ex)
            {
                
                throw (ex);
            }
        }

        /// <summary>
        /// 使用从字节数组中读取的数据将字节块写入 IsolatedStorageFileStream 对象
        /// </summary>
        /// <param name="dirName"></param>
        /// <param name="fileName"></param>
        /// <param name="bytes"></param>
        public static string WriteStringToFile(string dirName, string fileName, string strData)
        {
            if (string.IsNullOrEmpty(dirName) || string.IsNullOrEmpty(strData))
            {
                return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
            }
            if (!DirectoryExist(dirName))
            {
                string rCode = CreateDirectory(dirName);
                if (rCode != WPResultCode.RESTLT_OK)
                {
                    return rCode;
                }
            }

            StringBuilder sb = new StringBuilder();
            sb.Append(dirName);
            sb.Append("\\");
            sb.Append(fileName);

            return WriteStringToFile(sb.ToString(), strData);
        }

        /// <summary>
        /// 使用从字节数组中读取的数据将字节块写入 IsolatedStorageFileStream 对象
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="bytes"></param>
        public static string AppendStreamToFile(string fileName, byte[] bytes)
        {
            try
            {
                WPFile file = new WPFile();
                if (!DiskSpaceIsEnough(bytes.Length))
                {
                    return WPResultCode.RESULT_DISK_SPACE_NOT_ENOUGH;
                }

                if (string.IsNullOrEmpty(fileName))
                {
                    return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
                }

                if (FileExist(fileName))
                {
                    if (file.AppendStreamToFile(fileName, bytes))
                    {
                        return WPResultCode.RESTLT_OK;
                    }
                }
                return WPResultCode.RESULT_FAULT;
            }
            catch (IsolatedStorageException ex)
            {
                
                throw (ex);
            }
        }

        /// <summary>
        /// 使用从字节数组中读取的数据将字节块写入 IsolatedStorageFileStream 对象
        /// </summary>
        /// <param name="dirName"></param>
        /// <param name="fileName"></param>
        /// <param name="bytes"></param>
        public static string AppendStreamToFile(string dirName, string fileName, byte[] bytes)
        {
            if (string.IsNullOrEmpty(dirName) || string.IsNullOrEmpty(fileName))
            {
                return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
            }

            if (!DirectoryExist(dirName))
            {
                string rCode = CreateDirectory(dirName);
                if (rCode != WPResultCode.RESTLT_OK)
                {
                    return rCode;
                }
            }

            StringBuilder sb = new StringBuilder();
            sb.Append(dirName);
            sb.Append("\\");
            sb.Append(fileName);

            return AppendStreamToFile(sb.ToString(), bytes);
        }

        /// <summary>
        /// 使用从字节数组中读取的数据将字节块写入 IsolatedStorageFileStream 对象
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="bytes"></param>
        public static string AppendStringToFile(string fileName, string strData)
        {
            try
            {
                WPFile file = new WPFile();
                if (!DiskSpaceIsEnough(Encoding.Unicode.GetByteCount(strData)))
                {
                    return WPResultCode.RESULT_DISK_SPACE_NOT_ENOUGH;
                }

                if (string.IsNullOrEmpty(fileName))
                {
                    return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
                }

                if (FileExist(fileName))
                {
                    if (file.AppendStringToFile(fileName, strData))
                    {
                        return WPResultCode.RESTLT_OK;
                    }
                }
                else
                {
                    CreateFile(fileName);
                    if (file.AppendStringToFile(fileName, strData))
                    {
                        return WPResultCode.RESTLT_OK;
                    }

                }
                return WPResultCode.RESULT_FAULT;
            }
            catch (IsolatedStorageException ex)
            {
                
                throw (ex);
            }
        }

        /// <summary>
        /// 使用从字节数组中读取的数据将字节块写入 IsolatedStorageFileStream 对象
        /// </summary>
        /// <param name="dirName"></param>
        /// <param name="fileName"></param>
        /// <param name="bytes"></param>
        public static string AppendStringToFile(string dirName, string fileName, string strData)
        {
            if (string.IsNullOrEmpty(dirName) || string.IsNullOrEmpty(fileName))
            {
                return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
            }

            if (!DirectoryExist(dirName))
            {
                string rCode = CreateDirectory(dirName);
                if (rCode != WPResultCode.RESTLT_OK)
                {
                    return rCode;
                }
            }

            StringBuilder sb = new StringBuilder();
            sb.Append(dirName);
            sb.Append("\\");
            sb.Append(fileName);

            return AppendStringToFile(sb.ToString(), strData);
        }

        /// <summary>
        /// 将字节从当前缓冲的 IsolatedStorageFileStream 对象复制到数组
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public static string ReadStreamFromFile(string fileName, ref byte[] buffer, int offset, int count)
        {
            try
            {
                WPFile file = new WPFile();
                if (string.IsNullOrEmpty(fileName))
                {
                    return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
                }

                if (FileExist(fileName))
                {
                    if (file.ReadStreamFromFile(fileName, ref buffer, offset, count))
                    {
                        return WPResultCode.RESTLT_OK;
                    }
                }
                return WPResultCode.RESULT_FAULT;
            }
            catch (IsolatedStorageException ex)
            {
                
                throw (ex);
            }
        }

        /// <summary>
        /// 将字节从当前缓冲的 IsolatedStorageFileStream 对象复制到数组
        /// </summary>
        /// <param name="dirName"></param>
        /// <param name="fileName"></param>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public static string ReadStreamFromFile(string dirName, string fileName, ref byte[] buffer, int offset, int count)
        {
            if (string.IsNullOrEmpty(dirName) || string.IsNullOrEmpty(fileName))
            {
                return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
            }

            StringBuilder sb = new StringBuilder();
            sb.Append(dirName);
            sb.Append("\\");
            sb.Append(fileName);

            return ReadStreamFromFile(sb.ToString(), ref buffer, offset, count);
        }

        /// <summary>
        /// 将字节从当前缓冲的 IsolatedStorageFileStream 对象复制到数组
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public static string ReadStringFromFile(string fileName, ref string strData)
        {
            try
            {
                WPFile file = new WPFile();
                if (string.IsNullOrEmpty(fileName))
                {
                    return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
                }

                if (FileExist(fileName))
                {
                    if (file.ReadStringFromFile(fileName, ref strData))
                    {
                        return WPResultCode.RESTLT_OK;
                    }
                }
                return WPResultCode.RESULT_FAULT;
            }
            catch (IsolatedStorageException ex)
            {
                
                //throw (ex);
                return null;
            }
        }

        /// <summary>
        /// 将字节从当前缓冲的 IsolatedStorageFileStream 对象复制到数组
        /// </summary>
        /// <param name="dirName"></param>
        /// <param name="fileName"></param>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public static string ReadStringFromFile(string dirName, string fileName, ref string strData)
        {
            if (string.IsNullOrEmpty(dirName) || string.IsNullOrEmpty(fileName))
            {
                return WPResultCode.RESULT_FILE_OR_DIR_NAME_NULL;
            }

            StringBuilder sb = new StringBuilder();
            sb.Append(dirName);
            sb.Append("\\");
            sb.Append(fileName);

            return ReadStringFromFile(sb.ToString(), ref strData);
        }

        /// <summary>
        /// 获取独立存储剩余空间
        /// </summary>
        /// <returns></returns>
        public static long GetAvailableFreeSpace()
        {
            try
            {
                WPFile file = new WPFile();
                return file.GetAvailableFreeSpace();
            }
            catch (Exception ex)
            {
                
                throw (ex);
            }
        }

        /// <summary>
        /// 剩余空间是否足
        /// </summary>
        /// <returns></returns>
        public static bool DiskSpaceIsEnough()
        {
            try
            {
                if (GetAvailableFreeSpace() >= 10240)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                
                throw (ex);
            }
        }

        /// <summary>
        /// 重载：剩余空间是否足
        /// </summary>
        /// <returns></returns>
        public static bool DiskSpaceIsEnough(long lBytes)
        {
            try
            {
                if (GetAvailableFreeSpace() >= lBytes)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                
                throw (ex);
            }
        }

        /// <summary>
        /// 获取文件大小
        /// </summary>
        /// <returns></returns>
        public static long GetFileSize(string fileName)
        {
            try
            {
                WPFile file = new WPFile();
                if (string.IsNullOrEmpty(fileName))
                {
                    return 0;
                }

                return file.GetFileSize(fileName);
            }
            catch (Exception ex)
            {
                
                throw (ex);
            }
        }

        /// <summary>
        /// 重载：获取文件大小
        /// </summary>
        /// <returns></returns>
        public static long GetFileSize(string dirName, string fileName)
        {
            if (string.IsNullOrEmpty(dirName) || string.IsNullOrEmpty(fileName))
            {
                return 0;
            }

            StringBuilder sb = new StringBuilder();
            sb.Append(dirName);
            sb.Append("\\");
            sb.Append(fileName);
            return GetFileSize(sb.ToString());
        }

    }



}
